package com.ruicar.afs.cloud.afscase.credittemplate.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.credittemplate.condition.CreditTemplateCondition;
import com.ruicar.afs.cloud.afscase.credittemplate.entity.CreditTemplate;
import com.ruicar.afs.cloud.afscase.credittemplate.service.CreditTemplateService;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.security.service.AfsUser;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jiaqi.guo
 * @version 1.0
 * @description: 模板接口
 * @created 2020/5/19 11:44
 */
@Slf4j
@RestController
@AllArgsConstructor
@Api("模板接口")
@RequestMapping("/creditTemplate")
public class CreditTemplateController {

    private CreditTemplateService creditTemplateService;

    @RequestMapping(value = "/queryTemplateList")
    @ApiOperation(value = "多条件分页获取信审模板信息数据--仅展示当前登录用户所维护数据")
    public IResponse<IPage<CreditTemplate>> queryTemplateList(@RequestBody QueryCondition<CreditTemplateCondition> creditTemplate) {
        //获取当前登录用户ID刷新可查看数据
        List<String> roleList = SecurityUtils.getRoles();
        //判断当前登录用户是否为enum配置roleCode
        String bool = creditTemplateService.queryResult(roleList);
        return IResponse.success(creditTemplateService.page(new Page(creditTemplate.getPageNumber(), creditTemplate.getPageSize()), Wrappers.<CreditTemplate>query().lambda()
                .like(StringUtils.isNotEmpty(creditTemplate.getCondition().getTemplateName()), CreditTemplate::getTemplateName, creditTemplate.getCondition().getTemplateName())
                .like(StringUtils.isNoneEmpty(creditTemplate.getCondition().getTemplateContent()), CreditTemplate::getTemplateContent, creditTemplate.getCondition().getTemplateContent())
                .eq(StringUtils.isNotEmpty(creditTemplate.getCondition().getUserId()), CreditTemplate::getUserId, creditTemplate.getCondition().getUserId())
                .like(StringUtils.isNotEmpty(creditTemplate.getCondition().getUserName()), CreditTemplate::getUserName, creditTemplate.getCondition().getUserName())
                .eq(CreditTemplate::getUserId, SecurityUtils.getUser().getId().toString())
        ));
    }

    @RequestMapping(value = "/deleteTemplate/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "通过id删除")
    @Transactional
    @SysLog("删除模板")
    public IResponse<Boolean> deleteAtom(@PathVariable String id) {
        CreditTemplate creditTemplate = creditTemplateService.getById(id);
        if (creditTemplate == null) {
            return new IResponse<Boolean>().setMsg("通过id删除数据成功");
        }
        creditTemplateService.removeById(id);
        return new IResponse<Boolean>().setMsg("通过id删除数据成功");
    }

    @PostMapping(value = "/addTemplate")
    @ApiOperation(value = "新增模板")
    @Transactional
    @SysLog("新增模板")
    public IResponse<Boolean> addTemplate(@RequestBody CreditTemplate creditTemplate) {

            //获取当前登陆用户名保存userId
            AfsUser user = SecurityUtils.getUser();
            creditTemplate.setUserId(user.getId().toString());
            creditTemplate.setUserName(user.getUserRealName());
            creditTemplateService.save(creditTemplate);

        return new IResponse<Boolean>().setMsg("新增模板成功");
    }

    @PostMapping(value = "/editTemplate")
    @ApiOperation(value = "修改模板")
    @Transactional
    @SysLog("修改模板")
    public IResponse<Boolean> editTemplate(@RequestBody CreditTemplate creditTemplate) {
        creditTemplateService.updateById(creditTemplate);
        return new IResponse<Boolean>().setMsg("修改模板成功");
    }

    @GetMapping("/listTemplateByCurrentUser")
    @ApiOperation("获取当前登录用户的所有信贷报告模板")
    public IResponse<CreditTemplate> listTemplateByCurrentUser() {
        return IResponse.success(creditTemplateService.list(
                Wrappers.<CreditTemplate>lambdaQuery()
                        .eq(CreditTemplate::getUserId, SecurityUtils.getUser().getId().toString())));
    }
}